import java.util.Arrays;

public class Leetcode6 {
    public String convert(String s, int numRows) {
        if (numRows==1) return s;
        char[][] chars = new char[numRows][s.length()];
        for (int i = 0; i < chars.length; i++) {
            for (int j = 0; j < chars[0].length; j++) {
                chars[i][j]=' ';
            }
        }
        int i = 0;
        int row = 0;
        int col = 0;
        while (i<s.length()){
            if (row==0&&row<numRows){
                while (row<numRows&&i<s.length())
                chars[row++][col] = s.charAt(i++);
            } else if (row==numRows){
                --row;
                while (row-1>=1&&i<s.length())
                chars[--row][++col] = s.charAt(i++);
                row--;
                col++;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int j = 0; j < chars.length; j++) {
            for (int k = 0; k < chars[0].length; k++) {
                if (chars[j][k]!=' ') stringBuilder.append(chars[j][k]);
            }
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        Leetcode6 leetcode6 = new Leetcode6();
        String paypalishiring = leetcode6.convert("PAYPALISHIRING", 4);
        System.out.println(paypalishiring);
    }
}
